//
//  OrderdetailHeader.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/21.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit
import SnapKit

//enum Type {
//    case WaitForPay
//    case WaitForGoods
//    case AccountPaid
//    case Caceled
//    case Finished
//}

class OrderdetailHeader: UITableViewHeaderFooterView {

//    var type: Type = Type.WaitForPay
    var heightConstraint: Constraint?
    
    // MARK: - life cycle
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
        initBaseLayout()
        layoutPageSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.addSubview(topBackView)
        self.addSubview(orderNum)
        self.addSubview(statusLabel)
        self.addSubview(topLineView)
        self.addSubview(logisticsBackView)
        logisticsBackView.addSubview(carImageView)
        logisticsBackView.addSubview(logisticsInfoLabel)
        logisticsBackView.addSubview(dateLabel)
        logisticsBackView.addSubview(arrowImageView)
        self.addSubview(middleBackView)
        self.addSubview(nameLabel)
        self.addSubview(addressImageView)
        self.addSubview(addressLabel)
        self.addSubview(bottomBackView)
        self.addSubview(listLabel)
        self.addSubview(bottomLineView)
    }
    
    func layoutPageSubviews() {
        topBackView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(self).offset(0)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(44)
        }
        
        orderNum.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(14)
            make.top.equalTo(self).offset(16)
            make.width.equalTo(141)
            make.height.equalTo(14)
        }
        
        statusLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.top.equalTo(self).offset(16)
            make.width.equalTo(56)
            make.height.equalTo(14)
        }
        
        topLineView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(orderNum.snp.bottom).offset(15)
            make.right.equalTo(self)
            make.height.equalTo(1)
        }
        
        logisticsBackView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(topLineView.snp.bottom).offset(0)
            make.right.equalTo(self).offset(0)
            heightConstraint = make.height.equalTo(56).constraint
        }
        
        carImageView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(12)
            make.top.equalTo(topLineView.snp.bottom).offset(16)
            make.width.equalTo(15)
            make.height.equalTo(14)
        }
        
        logisticsInfoLabel.snp.makeConstraints { (make) in
            make.left.equalTo(carImageView.snp.right).offset(6)
            make.top.equalTo(topLineView.snp.bottom).offset(12)
            make.width.equalTo(310/375*SCREEN_WIDTH)
            make.height.equalTo(16)
        }
        
        dateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(34)
            make.top.equalTo(logisticsInfoLabel.snp.bottom).offset(2)
            make.width.equalTo(111)
            make.height.equalTo(16)
        }
        
        arrowImageView.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-16)
            make.top.equalTo(topLineView.snp.bottom).offset(23)
            make.width.equalTo(6)
            make.height.equalTo(12)
        }
        
        middleBackView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(logisticsBackView.snp.bottom).offset(12)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(80)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(33)
            make.top.equalTo(middleBackView).offset(17)
            make.width.equalTo(200)
            make.height.equalTo(12)
        }
        
        addressImageView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(14)
            make.top.equalTo(nameLabel.snp.bottom).offset(10)
            make.width.equalTo(11)
            make.height.equalTo(15)
        }
        
        addressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(addressImageView.snp.right).offset(7)
            make.top.equalTo(nameLabel.snp.bottom).offset(8)
            make.right.equalTo(self).offset(-42)
            make.height.equalTo(28)
        }
        
        bottomBackView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(middleBackView.snp.bottom).offset(16)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(40)
        }
        
        listLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(14)
            make.centerY.equalTo(bottomBackView)
            make.width.equalTo(89)
            make.height.equalTo(12)
        }
        
        bottomLineView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.bottom.equalTo(bottomBackView.snp.bottom).offset(0)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(1)
        }
    }
    
    func configUI(type: STATUS) {
        orderNum.text = "订单号：4859920392"
        logisticsInfoLabel.text = "【台州市】快件已到达 浙江台州分拨中心"
        dateLabel.text = "2016-12-23 09:37:27"
        nameLabel.text = "陈妞妞 18832891210"
        addressLabel.text = "北京市北京市朝阳区化工路双合中路双合家园C区3号院4号楼5单元1201"
        
        switch type {
        case .WAITFORGOODS:
            statusLabel.text = "待收货"
            logisticsBackView.isHidden = false
            break
        case .WAITFORPAY:
            statusLabel.text = "等待付款"
            logisticsBackView.isHidden = true
            bottomLineView.isHidden = true
            heightConstraint?.deactivate()
            break
        case .PAID:
            statusLabel.text = "已付款"
            logisticsBackView.isHidden = true
            bottomLineView.isHidden = true
            heightConstraint?.update(inset: 0)
            break
        case .CANCELED:
            statusLabel.text = "已取消"
            logisticsBackView.isHidden = true
            bottomLineView.isHidden = true
            heightConstraint?.update(inset: 0)
            break
        case .FINISHED:
            statusLabel.text = "已完成"
            logisticsBackView.isHidden = true
            bottomLineView.isHidden = true
            heightConstraint?.update(inset: 0)
            break
        default:
            statusLabel.text = "已完成"
            logisticsBackView.isHidden = true
            bottomLineView.isHidden = true
            heightConstraint?.update(inset: 0)
            break
        }

    }
    
    // MARK: - setters and getters
    var _topBackView: UIView!
    var topBackView: UIView {
        if _topBackView == nil {
            _topBackView = UIView()
            _topBackView.backgroundColor = UIColor.white
        }
        return _topBackView
    }
    
    var _orderNum: UILabel!
    var orderNum: UILabel {
        if _orderNum == nil {
            _orderNum = UILabel()
            _orderNum.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _orderNum.textColor = UIColor(hexString: "#252525")
        }
        return _orderNum
    }
    
    var _statusLabel: UILabel!
    var statusLabel: UILabel {
        if _statusLabel == nil {
            _statusLabel = UILabel()
            _statusLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _statusLabel.textColor = UIColor(hexString: "#B50834")
            _statusLabel.textAlignment = .right
        }
        return _statusLabel
    }
    
    var _topLineView: UIView!
    var topLineView: UIView {
        if _topLineView == nil {
            _topLineView = UIView()
            _topLineView.backgroundColor = UIColor(hexString: "#D1D1D1")
        }
        return _topLineView
    }
    
    var _logisticsBackView: UIView!
    var logisticsBackView: UIView {
        if _logisticsBackView == nil {
            _logisticsBackView = UIView()
            _logisticsBackView.backgroundColor = UIColor.white
        }
        return _logisticsBackView
    }
    
    var _carImageView: UIImageView!
    var carImageView: UIImageView {
        if _carImageView == nil {
            _carImageView = UIImageView()
            _carImageView.image = UIImage(named: "icon_express")
        }
        return _carImageView
    }
    
    var _logisticsInfoLabel: UILabel!
    var logisticsInfoLabel: UILabel {
        if _logisticsInfoLabel == nil {
            _logisticsInfoLabel = UILabel()
            _logisticsInfoLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _logisticsInfoLabel.textColor = UIColor(hexString: "#252525")
            _logisticsInfoLabel.numberOfLines = 0
        }
        return _logisticsInfoLabel
    }
    
    var _dateLabel: UILabel!
    var dateLabel: UILabel {
        if _dateLabel == nil {
            _dateLabel = UILabel()
            _dateLabel.font = UIFont(name: "PingFangSC-Regular", size: 10)
            _dateLabel.textColor = UIColor(hexString: "#2A2A2A")
        }
        return _dateLabel
    }
    
    var _arrowImageView: UIImageView!
    var arrowImageView: UIImageView {
        if _arrowImageView == nil {
            _arrowImageView = UIImageView()
            _arrowImageView.image = UIImage(named: "icon_arrow_r")
        }
        return _arrowImageView
    }
    
    var _topSpace: UIView!
    var topSpace: UIView {
        if _topSpace == nil {
            _topSpace = UIView()
            _topSpace.backgroundColor = UIColor(hexString: "#EEEEEE")
        }
        return _topSpace
    }
    
    var _middleBackView: UIView!
    var middleBackView: UIView {
        if _middleBackView == nil {
            _middleBackView = UIView()
            _middleBackView.backgroundColor = UIColor.white
        }
        return _middleBackView
    }
    
    var _nameLabel: UILabel!
    var nameLabel: UILabel {
        if _nameLabel == nil {
            _nameLabel = UILabel()
            _nameLabel.font = UIFont(name: "PingFangSC-Medium", size: 14)
            _nameLabel.textColor = UIColor(hexString: "#252525")
        }
        return _nameLabel
    }
    
    var _addressImageView: UIImageView!
    var addressImageView: UIImageView {
        if _addressImageView == nil {
            _addressImageView = UIImageView()
            _addressImageView.image = UIImage(named: "icon_address")
        }
        return _addressImageView
    }
    
    var _addressLabel: UILabel!
    var addressLabel: UILabel {
        if _addressLabel == nil {
            _addressLabel = UILabel()
            _addressLabel.font = UIFont(name: "PingFangSC-Regular", size: 12)
            _addressLabel.textColor = UIColor(hexString: "#666666")
            _addressLabel.numberOfLines = 0
        }
        return _addressLabel
    }
    
    var _bottomSpace: UIView!
    var bottomSpace: UIView {
        if _bottomSpace == nil {
            _bottomSpace = UIView()
            _bottomSpace.backgroundColor = UIColor(hexString: "#EEEEEE")
        }
        return _bottomSpace
    }
    
    var _bottomBackView: UIView!
    var bottomBackView: UIView {
        if _bottomBackView == nil {
            _bottomBackView = UIView()
            _bottomBackView.backgroundColor = UIColor.white
        }
        return _bottomBackView
    }
    
    var _listLabel: UILabel!
    var listLabel: UILabel {
        if _listLabel == nil {
            _listLabel = UILabel()
            _listLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _listLabel.textColor = UIColor(hexString: "#252525")
            _listLabel.text = "商品清单"
        }
        return _listLabel
    }
    
    var _bottomLineView: UIView!
    var bottomLineView: UIView {
        if _bottomLineView == nil {
            _bottomLineView = UIView()
            _bottomLineView.backgroundColor = UIColor(hexString: "#D1D1D1")
        }
        return _bottomLineView
    }

}
